package com.freewater.javabase.demo.network.netaddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class SpamCheck {
    private static final String BLACK_HOLE = "sbl.spamhaus.org";

    public static void main(String[] args) {
        final String address = "www.baidu.com";
        final boolean spammer = isSpammer(address);
        if (spammer) {
            System.out.println(address + " is a know spammer");
        } else {
            System.out.println(address + " appears legitimate");
        }
    }

    public static boolean isSpammer(String address) {
        try {
            final InetAddress inetAddress = InetAddress.getByName(address);
            final byte[] addressBytes = inetAddress.getAddress();
            String query = BLACK_HOLE;
            for (byte octet : addressBytes) {
                int unsignedByte = octet < 0 ? octet + 256 : octet;
                query = unsignedByte + "." + query;
            }
            System.out.println(query);
            final InetAddress response = InetAddress.getByName(query);
            return true;
        } catch (UnknownHostException e) {
            return false;
        }
    }
}
